﻿using Common;
using Common.Dtos;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sigma.Admin.Client.Services
{
    public class LoginService : ILoginService
    {
        private readonly HttpRestClient _client;
        private readonly string serviceName = "Login";

        public LoginService(HttpRestClient client)
        {
            _client = client;
        }

        public async Task<ApiResponse> Login(LoginUserDto user)
        {
            BaseRequest request = new BaseRequest();
            request.Method = RestSharp.Method.POST;
            request.Route = $"api/{serviceName}/Login";
            request.Parameter = user;
            var res = await _client.ExecuteAsync(request);
            if (res.Status)
            {
                var dto = JsonConvert.DeserializeObject<UsersDto>(res.Result.ToString());
                AppSession.Instance.Token = dto?.Token ?? string.Empty;
                AppSession.Instance.UserName = dto?.UserName ?? string.Empty;
            }
            return res;
        }

    }
}
